package org.com.gamemall.Config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolConfig {
    @Bean("voucherTaskExecutor")
    public ThreadPoolTaskExecutor voucherTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(5);
        executor.setQueueCapacity(30);
        executor.setThreadNamePrefix("Voucher-MQ-");
        executor.setDaemon(true);
        executor.setWaitForTasksToCompleteOnShutdown(true); // 重要！
        executor.setAwaitTerminationSeconds(30);
        //拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setAllowCoreThreadTimeOut( true);
        executor.initialize();
        return executor;
    }
}
